var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

var ContentTypeSchema = new mongoose.Schema({
	title:String,	//类别标题
	bannerimage:String,	//类别banner图
	isdel:Boolean,	//类别是否被删除
	display:Boolean,	//类别是否显示
	contents:[
		{
			type:ObjectId,
			ref:'Content'
		}
	],
	meta:{
		createAt:{
			type:Date,
			default:Date.now()
		},
		updateAt:{
			type:Date,
			default:Date.now()
		}
	}
});

ContentTypeSchema.pre('save',function(next){
	if(this.isNew){
		this.meta.createAt = this.meta.updateAt=Date.now();
		this.isdel = false;
	}else{
		this.meta.updateAt = Date.now();
	}
	next();
});

ContentTypeSchema.statics = {
	fetch:function(cb){
		return this
			.find({isdel:false})
			.exec(cb);
	},
	findById:function(id,cb){
		return this
			.findOne({_id:id})
			.exec(cb);
	},
	delete:function(id,cb){
		console.log(id+"Delete");
		return this
			.findOne({_id:id})
			.remove();
	}
}

module.exports = ContentTypeSchema;